<template>
  <div>
    <!-- 注册双击事件 -->
    <div class="my-tag" @dblclick="open">
      <input
        class="input"
        type="text"
        placeholder="输入标签"
        v-if="edit"
        v-ccc
        @blur="close"        
        v-model="tag"
        @keydown.enter="close" />
      <!-- 键盘回车事件 -->
      <div  class="text" v-else>{{value}}</div>
    </div>
  </div>
</template>

<script>
export default {
  props:['value'],
  data() {
    return {
      edit: false, //表示编辑的状态
    }
  },
  methods: {
    open() {
      this.edit = true
      this.tag=this.value//双击的时候显示
    },
    close(){
      this.edit=false
      this.$emit('input',this.tag)
    }
  },
  directives: {
    //自定义指令获取焦点
    ccc: {
      inserted(el) {
        el.focus()
      },
    },
  }
}
</script>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
